package cn.jiaqiang.chatgpt.data.domain.openai.service.rule.impl.filter;

import cn.jiaqiang.chatgpt.data.domain.openai.annotation.LogicStrategy;
import cn.jiaqiang.chatgpt.data.domain.openai.model.aggregates.ChatProcessAggregate;
import cn.jiaqiang.chatgpt.data.domain.openai.model.entity.RuleLogicEntity;
import cn.jiaqiang.chatgpt.data.domain.openai.model.entity.UserAccountEntity;
import cn.jiaqiang.chatgpt.data.domain.openai.model.valobj.LogicCheckTypeVO;
import cn.jiaqiang.chatgpt.data.domain.openai.model.valobj.UserAccountStatusVO;
import cn.jiaqiang.chatgpt.data.domain.openai.service.rule.ILogicFilter;
import cn.jiaqiang.chatgpt.data.domain.openai.service.rule.factory.DefaultLogicFactory;
import cn.jiaqiang.chatgpt.data.types.common.RedisConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @description 账户校验
 */
@Slf4j
@Component
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.ACCOUNT_STATUS)
public class AccountStatusFilter implements ILogicFilter<UserAccountEntity> {


    @Override
    public RuleLogicEntity<ChatProcessAggregate> filter(ChatProcessAggregate chatProcess, UserAccountEntity data) throws Exception {

        log.info("userId = {} 开始进行【账户状态拦截】", chatProcess.getUserId());
        // 账户可用，直接放行
        if (UserAccountStatusVO.AVAILABLE.equals(data.getUserAccountStatusVO())) {
            log.info("userId = {}, 【账户状态拦截】结果: {}", chatProcess.getUserId(), LogicCheckTypeVO.SUCCESS.getInfo());
            return RuleLogicEntity.<ChatProcessAggregate>builder()
                    .type(LogicCheckTypeVO.SUCCESS)
                    .data(chatProcess)
                    .build();
        }

        log.info("userId = {}, 【账户状态拦截】结果: {} -> 账户状态: {}",
                chatProcess.getUserId(), LogicCheckTypeVO.REFUSE.getInfo(), UserAccountStatusVO.FREEZE.getInfo());
        return RuleLogicEntity.<ChatProcessAggregate>builder()
                .info("您的账户已冻结，暂时不可使用。如果有疑问，可以联系客户解冻账户。")
                .type(LogicCheckTypeVO.REFUSE)
                .data(chatProcess)
                .build();
    }

}
